<!doctype html>
<html lang="zh-cn">
<head>

    <meta charset="utf-8">
    <meta name="generator" content="Hugo 0.57.2" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>常用加密/签名算法 | The Sky of OtsWang</title>
    <meta property="og:title" content="常用加密/签名算法 - The Sky of OtsWang">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content="2019-11-26T11:23:43&#43;08:00">
        
        
    <meta property="article:modified_time" content="2019-11-26T11:23:43&#43;08:00">
        
    <meta name="Keywords" content="golang,go语言,otswang,java,博客,python">
    <meta name="description" content="常用加密/签名算法">
        
    <meta name="author" content="OtsWang">
    <meta property="og:url" content="https://otswang.gitee.io/hugo/post/other/codec/">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">

    <link rel="stylesheet" href="/hugo/css/normalize.css">
    
        <link rel="stylesheet" href="/hugo/css/prism.css">
    
    <link rel="stylesheet" href="/hugo/css/style.css">
    <script type="text/javascript" src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>

    


    
    
</head>

<body>
<header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://otswang.gitee.io/hugo/">
                        The Sky of OtsWang
                    </a>
                
                <p class="description">擅长写HelloWorld的小小码农</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    
                    
                    <a  href="https://otswang.gitee.io/hugo/" title="Home">Home</a>
                    
                    <a  href="https://otswang.gitee.io/hugo/tags/" title="Tags">Tags</a>
                    
                    <a  href="https://otswang.gitee.io/hugo/categories/" title="Categories">Categories</a>
                    
                    <a  href="https://otswang.gitee.io/hugo/archives/" title="Archives">Archives</a>
                    
                    <a  href="https://otswang.gitee.io/hugo/about/" title="About">About</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>


<div id="body">
    <div class="container">
        <div class="col-group">

            <div class="col-8" id="main">
                <div class="res-cons">
                    <article class="post">
                        <header>
                            <h1 class="post-title">常用加密/签名算法</h1>
                        </header>
                        <date class="post-meta meta-date">
                            2019年11月26日
                        </date>
                        
                        <div class="post-meta">
                            <span>|</span>
                            
                                <span class="meta-category"><a href="https://otswang.gitee.io/hugo/categories/other">Other</a></span>
                            
                        </div>
                        
                        
                        
                        <div class="post-content">
                            <p>MD5，sha256，base64等算法</p>

<p>数字摘要：  MD5， sha</p>

<p>对称加密： des， aes</p>

<p>非对称加密： RSA</p>

<pre><code class="language-java">@Test
public void test01() {
    String msg = &quot;原文l&quot;;

    System.out.println(&quot;md5:&quot;);
    try {
        MessageDigest md = MessageDigest.getInstance(&quot;md5&quot;);
        byte[] md5bytes = md.digest(msg.getBytes());
        System.out.println(Hex.encodeHex(md5bytes));

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    System.out.println(DigestUtils.md5Hex(msg.getBytes()));

    System.out.println();

    System.out.println(&quot;sha&quot;);
    try {
        MessageDigest md = MessageDigest.getInstance(&quot;sha&quot;);
        byte[] shaByte = md.digest(msg.getBytes());
        System.out.println(Hex.encodeHex(shaByte));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    System.out.println(DigestUtils.sha1Hex(msg));
    System.out.println();

    System.out.println(&quot;sha256&quot;);
    try {
        MessageDigest md = MessageDigest.getInstance(&quot;sha-256&quot;);
        byte[] shaByte = md.digest(msg.getBytes());
        System.out.println(Hex.encodeHex(shaByte));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    System.out.println(DigestUtils.sha256Hex(msg));
    System.out.println();

    System.out.println(&quot;base64&quot;);
    try {
        BASE64Encoder encoder = new BASE64Encoder();
        String enStr = encoder.encode(msg.getBytes());
        System.out.println(enStr);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] deByte = decoder.decodeBuffer(enStr);
        System.out.println(new String(deByte));
    } catch (IOException e) {
        e.printStackTrace();
    }
    {
        String enStr = Base64.encodeBase64String(msg.getBytes());
        System.out.println(enStr);
        byte[] deByte = Base64.decodeBase64(enStr);
        System.out.println(new String(deByte));
    }
    System.out.println();

}
</code></pre>

<pre><code>md5:
7e285b6a33e000c57f0449420f5adc40
7e285b6a33e000c57f0449420f5adc40

sha
2d62a1dc3bde5a973f6d33d4b8dd085d4d45e894
2d62a1dc3bde5a973f6d33d4b8dd085d4d45e894

sha256
46c32b1d91820175c10e625f7de461f198f915da098b1f893ef21580137e99fd
46c32b1d91820175c10e625f7de461f198f915da098b1f893ef21580137e99fd

base64
5Y6f5paHbA==
原文l
5Y6f5paHbA==
原文l
</code></pre>
                        </div>

                        


                        


                        <div class="post-meta meta-tags">
                            
                            <ul class="clearfix">
                                
                                <li><a href="https://otswang.gitee.io/hugo/tags/codec">codec</a></li>
                                
                            </ul>
                            
                        </div>
                    </article>
                    
    

    
    
                </div>
            </div>
            <div id="secondary">

    <section class="widget">
        <form id="search" action="//www.google.com/search" method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://otswang.gitee.io/hugo/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>

    
    <div class="clear">
        <div class="toc-article">
            <div class="toc-title">文章目录</dixsv>
            
        </div>
    </div>
    

</div>
        </div>
    </div>
</div>
<footer id="footer">
    <div class="container">
        &copy; 2020 <a href="https://otswang.gitee.io/hugo/">The Sky of OtsWang By OtsWang</a>.
        Powered by <a rel="nofollow noreferer noopener" href="https://gohugo.io" target="_blank">Hugo</a>.
        <a href="https://www.flysnow.org/" target="_blank">Theme</a> based on <a href="https://github.com/Dudiao137/maupassant-hugo" target="_blank">maupassant-ots</a>.
        
    </div>
</footer>


    <script type="text/javascript">
    
    (function(){
        $("pre code").parent().addClass("line-numbers")
    }())

    window.MathJax = {
        tex2jax: {
            inlineMath: [ ['$','$'] ],
            processEscapes: true
        }
    };
    </script>
    <script type="text/javascript" src="/hugo/js/prism.js" async="true"></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src="/hugo/js/totop.js?v=0.0.0" async=""></script>







 
 <script src="https://mermaidjs.github.io/scripts/mermaid.min.js"></script>
 <script>
       mermaid.initialize({ startOnLoad: true });
 </script>
</body>
</html>
